import torch
import torch.nn as nn
import torch.nn.init as init
import torch.nn.functional as F
from attention.self_attention import SelfAttentionDot

# from model.self_attention import SelfAttentionDot
USE_CUDA = torch.cuda.is_available()
device = torch.device("cuda" if USE_CUDA else "cpu")


class _DenseLayer(nn.Sequential):
    def __init__(self, num_input_features, growth_rate, bn_size, drop_rate):
        super(_DenseLayer, self).__init__()
        self.add_module('norm1', nn.BatchNorm3d(num_input_features, momentum=0.5)),
        self.add_module('relu1', nn.ReLU(inplace=True)),
        # self.add_module('conv1', nn.Conv3d(num_input_features, bn_size *
        #                                    growth_rate, kernel_size=1, stride=1, bias=False)),
        self.add_module('conv1', nn.Conv3d(num_input_features, bn_size *growth_rate,
                                          kernel_size=3, stride=1, padding=1, bias=False)),
        self.add_module('norm2', nn.BatchNorm3d(bn_size * growth_rate, momentum=0.5)),
        self.add_module('relu2', nn.ReLU(inplace=True)),
        self.add_module('conv2', nn.Conv3d(bn_size * growth_rate, growth_rate,
                                           kernel_size=3, stride=1, padding=1, bias=False)),
        self.drop_rate = drop_rate

    def forward(self, x):
        new_features = super(_DenseLayer, self).forward(x)
        if self.drop_rate > 0:
            new_features = F.dropout(new_features, p=self.drop_rate, training=self.training)
        return torch.cat([x, new_features], 1)


class _DenseBlock(nn.Sequential):
    def __init__(self, num_layers, num_input_features, bn_size, growth_rate, drop_rate):
        super(_DenseBlock, self).__init__()
        for i in range(num_layers):
            layer = _DenseLayer(num_input_features + i * growth_rate, growth_rate, bn_size, drop_rate)
            self.add_module('denselayer%d' % (i + 1), layer)


class _Transition(nn.Sequential):
    def __init__(self, num_input_features, num_output_features):
        super(_Transition, self).__init__()
        self.add_module('norm', nn.BatchNorm3d(num_input_features, momentum=0.5))
        self.add_module('relu', nn.ReLU(inplace=True))
        # self.add_module('conv', nn.Conv3d(num_input_features, num_output_features,
        #                                   kernel_size=1, stride=1, bias=False))
        self.add_module('conv', nn.Conv3d(num_input_features, num_output_features,
                                          kernel_size=3, stride=1, padding=1, bias=False))
        self.add_module('pool', nn.MaxPool3d(kernel_size=(1, 2, 2), stride=(1, 2, 2)))
        # self.add_module('pool', nn.AvgPool3d(kernel_size=(1, 2, 2), stride=(1, 2, 2)))

class LipNet(nn.Module):
    def __init__(self, C, growth_rate=16, num_init_features=32, bn_size=4,drop_rate=0.0,o_dropout=0.5):
        super(LipNet, self).__init__()
        self.features = nn.Sequential(
            nn.Conv3d(1, 16, kernel_size=(3, 5, 5), stride=(1, 2, 2), padding=(1, 2, 2), bias=False),
            nn.BatchNorm3d(16, momentum=0.5),
            nn.ReLU(True),
            nn.MaxPool3d(kernel_size=(1, 2, 2), stride=(1, 2, 2)),

            nn.Conv3d(16, 32, kernel_size=(3, 5, 5), stride=(1, 1, 1), padding=(1, 2, 2), bias=False),
            nn.BatchNorm3d(32, momentum=0.5),
            nn.ReLU(True),
            nn.MaxPool3d(kernel_size=(1, 2, 2), stride=(1, 2, 2)),
        )
        block_config = (4, 4)
        num_features = num_init_features
        for i, num_layers in enumerate(block_config):

            block = _DenseBlock(num_layers=num_layers, num_input_features=num_features,
                                bn_size=bn_size, growth_rate=growth_rate, drop_rate=drop_rate)
            self.features.add_module('denseblock%d' % (i + 1), block)

            num_features = num_features + num_layers * growth_rate
            if i != len(block_config) - 1:
                trans = _Transition(num_input_features=num_features, num_output_features=num_features // 2)
                self.features.add_module('transition%d' % (i + 1), trans)
                num_features = num_features // 2
        self.features.add_module('norm5', nn.BatchNorm3d(num_features, momentum=0.5))

        # T B C*H*W
        self.gru1 = nn.GRU(112 * 3 * 6, 256, bidirectional=True,batch_first=True)
        self.drp1 = nn.Dropout(o_dropout)
        # T B F

        self.gru2 = nn.GRU(512, 256, 1, bidirectional=True,batch_first=True)
        self.drp2 = nn.Dropout(o_dropout)


        # T B V
        self.pred = nn.Linear(512*2, C)
        self.bn_norm = nn.BatchNorm1d(20, momentum=0.5)
        self.bn_norm1 = nn.BatchNorm1d(20, momentum=0.5)
        self.bn_norm2 = nn.BatchNorm1d(512 * 2, momentum=0.5)
        self.attention = SelfAttentionDot(512, 512)

    def forward(self, x):
        self.gru1.flatten_parameters()
        self.gru2.flatten_parameters()
        x = self.features(x)  # B C T H W
        x = x.permute(0, 2, 1, 3, 4).contiguous()  # T B C H W
        x = x.view(x.size(0), x.size(1), -1)
        # x = self.bn_norm(x)
        x, _ = self.gru1(x)
        x = self.bn_norm1(x)
        x = self.drp1(x)

        x, _ = self.gru2(x)

        att_out = self.attention(x)
        pool_out = x.transpose(1, 2)
        pool_out = F.max_pool1d(pool_out, pool_out.size(2)).squeeze(2)
        out = torch.cat((att_out, pool_out), dim=1)
        out = self.bn_norm2(out)
        out = self.drp2(out)
        out = self.pred(out).log_softmax(-1)

        return out


class LipSeqLoss(nn.Module):
    def __init__(self):
        super(LipSeqLoss, self).__init__()
        self.criterion = nn.NLLLoss(reduction='none')

    def forward(self, input, length, target):
        loss = []
        transposed = input.transpose(0, 1).contiguous()
        # transposed = input
        for i in range(transposed.size(0)):
            loss.append(self.criterion(transposed[i,], target.squeeze(1)).unsqueeze(1))
        loss = torch.cat(loss, 1)

        # GPU version
        mask = torch.zeros(loss.size(0), loss.size(1)).float().cuda()
        # Cpu version
        #         mask = torch.zeros(loss.size(0), loss.size(1)).float()

        for i in range(length.size(0)):
            L = min(mask.size(1), length[i])
            mask[i, L - 1] = 1.0
        loss = (loss * mask).sum() / mask.sum()
        return loss


if __name__ == '__main__':
    from DataLoader.Picture_Process import file_2_allpicture

    path = r"D:\XW_Bank\LipRecognition\train\lip_100_50_train\000c43e99bdceb93a39e729ffc38ac2a"
    input_p, pct_len = file_2_allpicture(path)
    model = LipNet(313).cuda()
    src = input_p.cuda()
    output = model(src)
    print(output.shape)
    length = torch.LongTensor([9]).cuda()
    print()

    # loss_func = LipSeqLoss()
    # loss = loss_func(output, torch.LongTensor([9]).cuda(), torch.LongTensor([[200]]).cuda())
    # print(loss)
